package system

import (
	"faker/project-public/common/response"
	"faker/project-public/global"
	sysResponse "faker/project-public/model/response"
	"faker/project-public/model/sys"
	"faker/project-public/request"
	"faker/project-public/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)

type SysRoleApi struct {
}

// CreateRole  创建Role
func (r *SysRoleApi) CreateRole(c *gin.Context) {
	var addRole request.AddRole
	err := c.ShouldBindJSON(&addRole)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}

	err = utils.Verify(addRole, utils.RoleVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	var roleData = &sys.SysRole{Name: addRole.Name, CreateTime: int(time.Now().Unix()), UpdateTime: int(time.Now().Unix())}
	authBack, err := roleService.CreateRole(*roleData)
	if err != nil {
		global.FAKER_LOG.Error("创建失败!", zap.Error(err))
		response.ErrorWithMessage("创建失败"+err.Error(), c)
		return
	}
	response.SuccessWithDetailed(sysResponse.SysAuthorityResponse{Role: authBack}, "创建成功", c)

}

// GetRoleInfo 获取Role信息
func (r *SysRoleApi) GetRoleInfo(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	GroupInfo, err := roleService.GetRoleInfo(idInfo.ID)
	response.SuccessWithDetailed(gin.H{"Role_info": GroupInfo}, "查询成功", c)
}

// GetRoleList 获取Group列表
func (r *SysRoleApi) GetRoleList(c *gin.Context) {
	var pageInfo request.PageInfo
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo, utils.PageVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	list, total, err := roleService.GetRoleList(pageInfo)
	response.SuccessWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "查询成功", c)
}

// SetRoleInfo 更新Role信息
func (r *SysRoleApi) SetRoleInfo(c *gin.Context) {
	var setRole sys.SysRole
	err := c.ShouldBindJSON(&setRole)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(setRole, utils.RoleVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = roleService.SetRole(setRole)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("role更新失败", zap.Error(err))
		return
	}
	response.SuccessWithMessage("更新成功", c)
}

// DeleteRole 删除角路径
func (r *SysRoleApi) DeleteRole(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = roleService.DeleteRole(idInfo.ID)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("删除失败", zap.Error(err))
		return
	}
	response.SuccessWithMessage("删除成功", c)
}

// GetRoles 获取总角色列表
func (r *SysRoleApi) GetRoles(c *gin.Context) {
	list, err := roleService.GetRoles()
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("获取失败", zap.Error(err))
		return
	}
	response.SuccessWithDetailed(gin.H{"roles": list}, "获取成功", c)
}

// GetRoleRules 获取某个角色的功能列表
func (r *SysRoleApi) GetRoleRules(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	list, err := roleService.GetRoleRules(idInfo.ID)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("获取失败", zap.Error(err))
		return
	}
	response.SuccessWithDetailed(gin.H{"roles": list}, "获取成功", c)
}

// SetRoleRules 权限分配
func (r *SysRoleApi) SetRoleRules(c *gin.Context) {
	var idsInfo request.IdsStruct
	err := c.ShouldBindJSON(&idsInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = roleService.SetRoleRules(idsInfo.ID, idsInfo.RuleIds)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("权限分配失败", zap.Error(err))
		return
	}
	response.SuccessWithMessage("权限分配成功", c)
}
